package logic

import (
	"web_app/dao/mysql"
	"web_app/models"
	"web_app/pkg/JWT"
	"web_app/pkg/snowflake"
)

func SignUp(p *models.ParamsSignUp) (err error) {
	//判断用户是否存在
	if err := mysql.CheckUserExist(p.UserName); err != nil {
		return err
	}
	//构建用户ID
	userID := snowflake.GenID()

	//构建用户实列
	user := &models.User{
		UserID:   userID,
		UserName: p.UserName,
		Password: p.Password,
	}
	//插入用户数据
	return mysql.InsertUser(user)
}
func Login(p *models.ParamsLogin) (token string, err error) {
	//根据用户名密码匹配账户是否存在
	user := &models.User{
		UserName: p.UserName,
		Password: p.Password,
	}
	if err = mysql.GetUser(user); err != nil {
		return "", err
	}
	return JWT.GenToken(user.UserID, user.UserName)
}
